﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using CRM.Admin.Contracts.Contract.Customer;
using CRM.Admin.Contracts.Contract.Logs;
using CRM.Admin.Contracts.Dto.Customer;
using CRM.Admin.Contracts.Dto.Logs;
using CRM.Admin.Contracts.Model.Customer;
using CRM.Admin.Services.Service.Logs;
using KingsSharp.Common;
using KingsSharp.Common.Extensions;
using KingsSharp.Common.Filter;
using KingsSharp.Core.Data.Entity;
using KingsSharp.Core.Data.Extensions;
using KingsSharp.Web.Helper;
using KingsSharp.Web.UI;



namespace CRM.Admin.Areas.Customer.Controllers
{
    public class ProductController : Controller
    {
        public IProductService ProductService { get; set; }

        public ActionResult ProductIndex()
        {
            return View();
        }
        public ActionResult ProductAdd(Guid CustomerId)
        {
            ViewBag.CustomerId = CustomerId;
            return View();
        }
        public ActionResult ComponentEdit(Guid ComponentId)
        {
            var dto = ProductService.Components.FirstOrDefault(a => a.Id == ComponentId).MapTo<ComponentDto>();
            return View(dto);
        }
        public ActionResult ProductEdit(Guid CustomerId,Guid Id)
        {
            var dto = ProductService.Products.Where(a => a.CustomerId == CustomerId&&a.Id==Id).FirstOrDefault().MapTo<ProductDto>();
            ViewBag.CustomerId = CustomerId;
            
            return View(dto);
        }
        public ActionResult ProductInsert(ProductDto productDto)
        {
            productDto.CheckNotNull("productDto");
            var result = ProductService.Insert(productDto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "产品 “{0}” 添加成功。".FormatWith(productDto.Name);
            }
            return Json(new AjaxResult(msg, msgType));
        }
        public ActionResult ProductDelete(Guid UserId)
        {
            UserId.CheckNotEmpty("UserId");
            Guid[] id = new Guid[] { UserId };
            var result = ProductService.Delete(true, id);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "产删除成功。";
            }
            return Json(new AjaxResult(msg, msgType));
        }
        public ActionResult ProductUpdate(ProductDto productDto) 
        {
           
            productDto.CheckNotNull("productDto");
            var result = ProductService.Update(productDto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "产品 “{0}”更新成功。".FormatWith(productDto.Name);
            }
            return Json(new AjaxResult(msg, msgType));
        }

        public ActionResult ComponentAdd(Guid ProductId)
        {
            ViewBag.ProductId = ProductId;
            return View();
        }
        public ActionResult ComponentInsert(ComponentDto componentDto)
        {
            componentDto.CheckNotNull("componentDto");

            var result = ProductService.Insert(componentDto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "零件 “{0}” 添加成功。".FormatWith(componentDto.Name);
            }
            return Json(new AjaxResult(msg, msgType));
        }
        public ActionResult ComponentUpdate(ComponentDto componentDto)
        {
            componentDto.CheckNotNull("componentDto");
            var result = ProductService.Update(componentDto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "零件 “{0}” 更新成功。".FormatWith(componentDto.Name);
            }
            return Json(new AjaxResult(msg, msgType));

        }
        /// <summary>
        /// 显示零件列表
        /// </summary>
        /// <param name="ProductId">产品ID</param>
        /// <returns></returns>
        public ActionResult ComponentList(Guid ProductId)
        {
            ViewBag.ProductId = ProductId;
            return View();
        }
        public ActionResult ComponentDelete(Guid UserId, string UserName)
        {
            var result = ProductService.Delete(UserId);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "零件 “{0}” 删除成功。".FormatWith(UserName);
            }
            return Json(new AjaxResult(msg, msgType));
        }
     
        public ActionResult GetComponentGridData()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<Component, bool>> predicate = FilterHelper.GetExpression<Component>(request.FilterGroup);
            int total;
            var data = ProductService.Components.
                Where<Component, Guid>(predicate, request.PageCondition, out total).
                Where(a => a.IsDeleted == false).
                Select(dto => new
                {
                    dto.Id,
                    dto.Name,
                    dto.Size,
                    dto.DownSize
                }).ToList();
            return Json(new GridData<object>(data, total), JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetProductGridData()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<Product, bool>> predicate = FilterHelper.GetExpression<Product>(request.FilterGroup);
            int total;
            var data = ProductService.Products.
                Where<Product, Guid>(predicate, request.PageCondition, out total).
                Where(a => a.IsDeleted == false).
                Select(dto => new
                {
                    dto.Id,
                    dto.Name,
                    dto.Consumption,
                    dto.NeedSteelWeight,
                    dto.SaleWeight,
                    dto.UseSteelType
                }).ToList();
            return Json(new GridData<object>(data, total), JsonRequestBehavior.AllowGet);
        }

        public ActionResult ProductCheck(bool Multi=false)
        {
            ViewBag.Multi = Multi.ToString().ToLower();
            return View();
        }

        public ActionResult ComponentCheck(bool Multi = false)
        {
            ViewBag.Multi = Multi.ToString().ToLower();
            return View();
        }
    }
}